package com.ucar.ui.base;

import android.app.Activity;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewStub;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.ucar.R;
import com.ucar.util.SpUtil;
import com.umeng.analytics.MobclickAgent;

public abstract class BaseFragmentActivity extends FragmentActivity {

	private TextView tvLeft;
	private ImageView ivLeft;
	private TextView tvTitle;
	private ImageView ivRight;
	private TextView tvRight;
	
	protected LayoutInflater mInflater = null;
	
	protected int finishAnimId = 0;

	protected Toast mShortToast = null;

	protected Toast mLongToast = null;
	
	protected SpUtil mSpUtil;
	
	protected Dialog progressDialog;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		mInflater = LayoutInflater.from( this );
		mSpUtil = SpUtil.getInstance(this);
		
		setContentView( R.layout.activity_base );
	}
	
	/**
	 * 设置标题以及内容布局
	 * @param layoutResId
	 * @param titleId
	 */
	public void setBaseTitleContent( int layoutResId, int titleId )
	{
		setBaseTitleContent( layoutResId, getString( titleId ) );
	}
	
	/**
	 * 设置标题以及内容布局
	 * @param layoutResId
	 * @param title
	 */
	public void setBaseTitleContent( int layoutResId, String title )
	{
		ViewStub layoutContent = find( R.id.content );
		layoutContent.setLayoutResource( layoutResId );
		layoutContent.inflate();
		
		tvLeft = find( R.id.tv_left );
		ivLeft = find( R.id.iv_left );
		tvTitle = find( R.id.tv_title );
		tvRight = find( R.id.tv_right );
		ivRight = find( R.id.iv_right );
		
		setTitle( title );
		
		// 调用顺序
		onInit();
		onFindViews();
		onInitViewData();
		onBindListener();
	}

	/**
	 * 查找view
	 * @param id
	 * @return
	 */
	@SuppressWarnings("unchecked")
	protected <T extends View> T find( int id )
	{
		return (T) findViewById( id );
	}
	
	/**
	 * 设置坐标文字
	 * @param resId
	 * @param listener
	 */
	public void setLeftText( int resId, OnClickListener listener )
	{
		ivLeft.setVisibility( View.GONE );
		tvLeft.setVisibility( View.VISIBLE );
		tvLeft.setText( resId );
		if ( listener != null )
		{
			tvLeft.setOnClickListener( listener );
		}
		else
		{
			tvLeft.setOnClickListener( new OnClickListener() {
				
				@Override
				public void onClick(View view) {
					finish();
				}
			} );
		}
	}
	
	/**
	 * 设置左边文字
	 * @param resId
	 * @param listener
	 */
	public void setLeftText( int resId )
	{
		setLeftText( resId, null );
	}
	
	/**
	 * 设置左边图片
	 * @param resId
	 * @param listener
	 */
	public void setLeftImage( int resId, OnClickListener listener )
	{
		ivLeft.setVisibility( View.VISIBLE );
		tvLeft.setVisibility( View.GONE );
		ivLeft.setImageResource( resId );
		if ( listener != null )
		{
			ivLeft.setOnClickListener( listener );
		}
		else
		{
			ivLeft.setOnClickListener( new OnClickListener() {
				
				@Override
				public void onClick(View view) {
					finish();
				}
			} );
		}
	}
	
	/**
	 * 设置左边图片
	 * @param resId
	 * @param listener
	 */
	public void setLeftImage( int resId )
	{
		setLeftImage( resId, null );
	}
	
	/**
	 * 设置右边文字
	 * @param resId
	 * @param listener
	 */
	public void setRightText( int resId, OnClickListener listener )
	{
		ivRight.setVisibility( View.GONE );
		tvRight.setVisibility( View.VISIBLE );
		tvRight.setText( resId );
		if ( listener != null )
		{
			tvRight.setOnClickListener( listener );
		}
	}
	
	/**
	 * 设置右边文字
	 * @param resId
	 * @param listener
	 */
	public void setRightText( int resId )
	{
		setRightText( resId, null );
	}
	
	/**
	 * 设置右边图片
	 * @param resId
	 * @param listener
	 */
	public void setRightImage( int resId, OnClickListener listener )
	{
		ivRight.setVisibility( View.VISIBLE );
		tvRight.setVisibility( View.GONE );
		ivRight.setImageResource( resId );
		if ( listener != null )
		{
			ivRight.setOnClickListener( listener );
		}
	}
	
	/**
	 * 设置右边图片
	 * @param resId
	 * @param listener
	 */
	public void setRightImage( int resId )
	{
		setRightImage( resId, null );
	}
	
	/**
	 * 设置标题
	 * @param title
	 */
	public void setTitle(String title) 
	{
		tvTitle.setText(title);
	}
	
	/**
	 * 隐藏左边
	 */
	public void hideLeft()
	{
		ivLeft.setVisibility( View.GONE );
		tvLeft.setVisibility( View.GONE );
	}
	/**
	 * 隐藏右边
	 */
	public void hideRight()
	{
		ivRight.setVisibility( View.GONE );
		tvRight.setVisibility( View.GONE );
	}

	/**
	 * 初始化 优先顺序：<br/>
	 * <font color=red>onInit();</font><br/>
	 * onFindViews();<br/>
	 * onInitViewData();<br/>
	 * onBindListener();<br/>
	 */
	public abstract void onInit();

	/**
	 * 查找控件 <br/>
	 * 优先顺序：<br/>
	 * onInit();<br/>
	 * <font color=red>onFindViews();</font><br/>
	 * onInitViewData();<br/>
	 * onBindListener();<br/>
	 */
	public abstract void onFindViews();

	/**
	 * 初始化控件内容 优先顺序：<br/>
	 * onInit();<br/>
	 * onFindViews();<br/>
	 * <font color=red>onInitViewData();</font><br/>
	 * onBindListener();<br/>
	 */
	public abstract void onInitViewData();

	/**
	 * 注册控件事件 优先顺序：<br/>
	 * onInit();<br/>
	 * onFindViews();<br/>
	 * onInitViewData();<br/>
	 * <font color=red>onBindListener();</font><br/>
	 */
	public abstract void onBindListener();

	/**
	 * 结束此activity时动画 从左边移到右边 <功能详细描述>
	 * 
	 * @see [类、类#方法、类#成员]
	 */
	public void finishWithLeftAnim() {
		finishAnimId = R.anim.from_left_out;
	}

	/**
	 * 结束此activity时动画 从上往下移动 <功能详细描述>
	 * 
	 * @see [类、类#方法、类#成员]
	 */
	public void finishWithDownAnim() {
		finishAnimId = R.anim.from_up_out;
	}

	/**
	 * Toast 短时间显示 <功能详细描述>
	 * 
	 * @param message
	 * @see [类、类#方法、类#成员]
	 */
	public void showToastShort(String message) {
		Toast.makeText(this, message, Toast.LENGTH_SHORT).show();
	}

	public void showToastShort(int id) {
		Toast.makeText(this, id, Toast.LENGTH_SHORT).show();
	}

	/**
	 * Toast 长时间显示 <功能详细描述>
	 * 
	 * @param message
	 * @see [类、类#方法、类#成员]
	 */
	public void showToastLong(String message) {
		Toast.makeText(this, message, Toast.LENGTH_LONG).show();
	}

	/**
	 * Toast <功能详细描述>
	 * 
	 * @param str资源ID
	 * @see [类、类#方法、类#成员]
	 */
	public void showToast(int strResID) {
		String msgStr = this.getResources().getString(strResID);
		showToastLong(msgStr);
	}

	/**
	 * 显示提示
	 * 
	 * @param message
	 */
	public void showToast(String message) {
		showToastLong(message);
	}

	protected Activity getActivity() {
		return this;
	}

	protected void runOnUiThreadSafety(final Runnable runnable) {
		Activity activity = getActivity();
		if (activity == null) {
			return;
		}
		if (runnable == null) {
			return;
		}
		try {
			activity.runOnUiThread(runnable);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	protected void toCloseProgressMsg() {
		runOnUiThreadSafety(new Runnable() {
			@Override
			public void run() {
				closeProgressDialog();
			}
		});
	}

	protected Dialog getProgressDialog(String msg) {
		ProgressDialog dialog = new ProgressDialog(getActivity());
		dialog.setMessage(msg);
		return dialog;
	}

	private void closeProgressDialog() {
		if (progressDialog != null && progressDialog.isShowing() && progressDialog.getWindow() != null) {
			progressDialog.dismiss();
		}
	}

	protected void toShowProgressMsg(final String msg) {
		runOnUiThreadSafety(new Runnable() {
			@Override
			public void run() {
				if (progressDialog != null && progressDialog.isShowing()) {
					if (progressDialog instanceof ProgressDialog) {
						((ProgressDialog) progressDialog).setMessage(msg);
					} else {
						try {
							progressDialog.getClass().getMethod("setMessage", String.class).invoke(progressDialog, msg);
						} catch (Exception e) {
							e.printStackTrace();
						}
					}
				} else {
					progressDialog = getProgressDialog(msg);
					progressDialog.show();
				}
			}
		});
	}
	
	/**
     * Intent跳转
     * 
     * @param target
     */
    protected void redirectActivity(Class<? extends Activity> target) {
        Intent it = new Intent( this, target );
        startActivity(it);
    }
    
    protected void redirectActivity(Class<? extends Activity> target, Bundle data) {
        Intent it = new Intent( this, target );
        it.putExtras(data);
        startActivity(it);
    }
	
	@Override
	protected void onResume() {
		super.onResume();
		MobclickAgent.onResume(this);
	}

	@Override
	protected void onPause() {
		super.onPause();
		MobclickAgent.onPause(this);
	}

	@Override
	public void finish() {
		super.finish();
		if (finishAnimId != 0) {
			overridePendingTransition(R.anim.none, finishAnimId);
		}
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
	}

	protected void addFragment(Fragment fragment, int resId, String tag) 
    {
		FragmentManager fragmentManager = getSupportFragmentManager();
		FragmentTransaction transaction = fragmentManager.beginTransaction();
		transaction.replace(resId, fragment, tag);
		transaction.commit();
	}
}
